<?php
namespace app\api\controller;

use app\BaseController;
use app\common\business\User;

class Login extends BaseController
{
    public function index()
    {
        if (!$this->request->isPost()) {
            return show(config('status.error'),'未知的请求');
        }
        $phoneNumber = input('param.phone_number','','trim');
        $code = input('param.code','','intval');
        $type = input('param.type','','intval');

        $data = [
            'phone_number' => $phoneNumber,
            'code' => $code,
            'type' => $type,
        ];

        $validate = new \app\api\validate\User();
        if (!$validate->scene('login')->check($data)) {
            return show(config('status.error'),$validate->getError());
        }

        try {
            $res = (new \app\common\business\User())->login($data);
        } catch (\think\Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
        if (!$res) {
            return show(config('status.error'),'登录失败');
        }
        return show(config('status.success'), '登录成功', $res);
    }
}